package com.kaifamiao.io.d0924;

import java.util.Objects;

public class Student extends Person {

    private static final long serialVersionUID = 2L;

    // transient 表示序列化时不会序列化这个字段
    private transient int age;

    private String name;

    private char gender;

    static int count;

    public static int staticField;

    public Student(){
        System.out.println("Student()");
    }

    Student(String name){
        System.out.println("Student(String)");
        this.name = name;
    }

    protected Student(int age, String name, char gender){
        System.out.println("Student构造");
        this.age = age;
        this.name = name;
        this.gender = gender;
    }

    public Student(Integer age, String name, char gender){
        System.out.println("Student构造1");
        this.age = age;
        this.name = name;
        this.gender = gender;
    }

    private void privateMethod(){}

    void packageMethod(){}

    protected void protectedMethod(){}

    public void publicMethod(){}

    @Override
    public String toString() {
        return "Student{" +
                "age=" + age +
                ", name='" + name + '\'' +
                ", gender=" + gender +
                ", count=" + count +
                '}';
    }

    /*
        自反性
        对称性
        传递性
        一致性
        对 null 永远是 false
     */
    @Override
    public boolean equals(Object obj){
        // 是同一个对象
        if (this == obj){
            return true;
        }

        if (obj != null){
            Class cla = obj.getClass();

            if (cla == Student.class){
                Student stu = (Student) obj;
                if(this.age == stu.age && this.count == stu.count
                        && this.gender == stu.gender
                        && Objects.equals(stu.name, this.name)){
                    return true;
                }
            }
        }

        // 是否属于 Student, Student 类以及其子类对象都属于 Student. 同类型
        if (obj instanceof Student stu){
            if(this.age == stu.age && this.count == stu.count
                    && this.gender == stu.gender
                    && Objects.equals(stu.name, this.name)){
                return true;
            }
        }

        return false;
    }

    @Override
    public int hashCode() {
        return Objects.hash(this.age, this.name, this.gender, this.count);
    }

    public void setName(String name){
        System.out.println("setName(String)");
        this.name = name;
    }

    public String getName(){
        return this.name;
    }

    public static void staticMethod(){
        System.out.println("staticMethod");
    }
}

class Person {

    private int a;

    protected int b;

    double height;

    public double weight;

}
